package schedule;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Process {
	String name;
	int serviceTime;
	int remainingTime;
	int elapsed;
	int round;
	String state;

	Process(String name, int serviceTime) {
		this.name = name;
		this.serviceTime = serviceTime;
		this.remainingTime = serviceTime;
		this.elapsed = 0;
		this.round = 0;
		this.state = "W";
	}
}

public class ProcessScheduler {
	public static void main(String[] args) {
		Queue<Process> readyQueue = new LinkedList<>();
		Queue<Process> finishedQueue = new LinkedList<>();

		Scanner scanner = new Scanner(System.in);

		// 从用户输入中获取进程信息
		System.out.println("1. round robin algorithm");
		System.out.println("result：");

		// 提示用户输入
		System.out.print("input name and service time (press Enter to finish): ");

		while (true) {
			String input = scanner.nextLine().trim();
			if (input.isEmpty()) {
				break;
			}

			if (input.startsWith("input name and service time")) {
				// 仅在开头输出一次提示
				continue;
			}

			String[] tokens = input.split("\\s+");
			if (tokens.length == 2) {
				String name = tokens[0];
				int serviceTime = Integer.parseInt(tokens[1]);
				readyQueue.add(new Process(name, serviceTime));
			} else {
				System.out.println("Invalid input. Please enter again.");
			}
		}

		// 时间片轮转调度算法
		timeSliceScheduling(readyQueue, finishedQueue);

		// 输出结果
		System.out.println("Name   elapsed time   remaining time   count     round       state");
		for (Process process : finishedQueue) {
			System.out.printf("%5s%15d%18d%13d%12d%12s\n",
					process.name, process.elapsed, process.remainingTime, process.round, 2, process.state);
		}

		System.out.println("Ready query: " + getReadyQueueNames(readyQueue));
		System.out.println("Finished query: " + getFinishedQueueNames(finishedQueue));

		// 关闭Scanner
		scanner.close();
	}

	// 时间片轮转调度算法
	private static void timeSliceScheduling(Queue<Process> readyQueue, Queue<Process> finishedQueue) {
		System.out.println("Round Robin Algorithm:");
		int timeSlice = 2; // 时间片大小为2

		Scanner scanner = new Scanner(System.in);

		while (!readyQueue.isEmpty()) {
			Process currentProcess = readyQueue.poll();

			System.out.println("Running: " + currentProcess.name);

			// 模拟运行
			if (currentProcess.remainingTime > timeSlice) {
				currentProcess.elapsed += timeSlice;
				currentProcess.remainingTime -= timeSlice;
				currentProcess.round += 1;
				currentProcess.state = "W";
				readyQueue.add(currentProcess); // 放回队列等待下次调度
			} else {
				currentProcess.elapsed += currentProcess.remainingTime;
				currentProcess.remainingTime = 0;
				currentProcess.round += 1;
				currentProcess.state = "F";
				finishedQueue.add(currentProcess);
			}

			// 输出中间结果
			System.out.println("Name   elapsed time   remaining time   count     round       state");
			for (Process process : finishedQueue) {
				System.out.printf("%5s%15d%18d%13d%12d%12s\n",
						process.name, process.elapsed, process.remainingTime, process.round, 2, process.state);
			}

			System.out.println("Ready query: " + getReadyQueueNames(readyQueue));
			System.out.println("Finished query: " + getFinishedQueueNames(finishedQueue));

			// 提示用户输入
			System.out.print("input name and service time (press Enter to finish): ");
		}

		// 关闭Scanner
		scanner.close();
	}

	// 获取就绪队列中进程的名称
	private static String getReadyQueueNames(Queue<Process> readyQueue) {
		StringBuilder names = new StringBuilder();
		for (Process process : readyQueue) {
			names.append(process.name).append(" ");
		}
		return names.toString().trim();
	}

	// 获取完成队列中进程的名称
	private static String getFinishedQueueNames(Queue<Process> finishedQueue) {
		StringBuilder names = new StringBuilder();
		for (Process process : finishedQueue) {
			names.append(process.name).append(" ");
		}
		return names.toString().trim();
	}
}
